package com.eastjet.maintenance.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eastjet.maintenance.domain.Documentation;
import com.eastjet.maintenance.domain.InboundDetails;
import com.eastjet.maintenance.domain.NormalWarehousing;
import com.eastjet.maintenance.feign.CodeDataFeing;
import com.eastjet.maintenance.mapper.NormalWarehousingMapper;
import com.eastjet.maintenance.service.NormalWarehousingService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 杨博文
 * @description 针对表【normal_warehousing(普通入库)】的数据库操作Service实现
 * @createDate 2024-11-25 09:23:38
 */
@Service
public class NormalWarehousingServiceImpl extends ServiceImpl<NormalWarehousingMapper, NormalWarehousing> implements NormalWarehousingService {

    @Autowired
    private NormalWarehousingMapper normalWarehousingMapper;
    @Autowired
    private InboundDetailsServiceImpl inboundDetailsServiceImpl;// 入库明细Service
    @Autowired
    private DocumentationServiceImpl documentationServiceImpl;// 文档Service
    @Autowired
    private CodeDataFeing codeDataFeing;

    @SneakyThrows
    @Override
    public Boolean InsertNormalWarehousing(Map<String, Object> map) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date datee = df.parse(df.format(new Date()));
        String code = codeDataFeing.test(new HashMap<String, String>() {{
            put("TableName", "normal_warehousing");
        }});
        NormalWarehousing p = new NormalWarehousing();
        p.setCode(code);
        p.setInboundTheme(map.get("inboundTheme").toString());
        p.setInboundType(map.get("inboundType").toString());
        p.setInboundDate(df.parse(map.get("inboundDate").toString()));
        p.setInboundApplicants(map.get("inboundApplicants").toString());
        p.setDepartment(map.get("department").toString());
        p.setWarehouse(map.get("warehouse").toString());
        p.setInboundPersonnel(map.get("inboundPersonnel").toString());
        p.setWarehouseAdministrator(map.get("warehouseAdministrator").toString());
        p.setDepartment2(map.get("department2").toString());
        p.setRemark(map.get("remark").toString());
        p.setCreatedBy(map.get("inboundApplicants").toString());
        p.setCreationTime(datee);
        p.setIs_delete(0);
        boolean save = this.save(p);
        if (save) {
            // 入库明细
            List<Map<String, Object>> list = (List<Map<String, Object>>) map.get("tableData");
            List<InboundDetails> inboundDetailsList = new ArrayList<>();
            list.forEach(item -> {
                item = (Map<String, Object>) item;
                InboundDetails t = new InboundDetails();
                t.setDiscrepancyCode(code);
                t.setEncode(((Map<?, ?>) item).get("code").toString());
                t.setName(((Map<?, ?>) item).get("name").toString());
                t.setSpecification(((Map<?, ?>) item).get("specification").toString());
                t.setType(((Map<?, ?>) item).get("type").toString());
                t.setBrand(((Map<?, ?>) item).get("brand").toString());
                t.setUnit(((Map<?, ?>) item).get("unit").toString());
                t.setInboundQuantity(((Map<?, ?>) item).get("amount").toString());
                t.setStorageLocation(((Map<?, ?>) item).get("location").toString());
                t.setRemark(((Map<?, ?>) item).get("remark2").toString());
                t.setDiscrepancy("入库");
                t.setCreatedBy(map.get("inboundApplicants").toString());
                t.setCreationTime(datee);
                t.setTombstone(0);
                inboundDetailsList.add(t);
            });
            Boolean b = inboundDetailsServiceImpl.InsertRu(inboundDetailsList);
            if (b) {
                //新增文档
                List<Object> documentationData = (List<Object>) map.get("documents");
                List<Documentation> documentationList = new ArrayList<>();
                //循环映射文档数据
                documentationData.forEach(item -> {
                    item = (Map<String, Object>) item;
                    Documentation doc = new Documentation();
                    doc.setCode(code);
                    doc.setFilename(((Map<?, ?>) item).get("name").toString());
                    doc.setUploadTime(datee);
                    doc.setUploader(map.get("usname").toString());
                    doc.setSize(((Map<?, ?>) item).get("size").toString());
                    doc.setEid(1);
                    doc.setCreationTime(datee);
                    doc.setCreatedBy(map.get("usname").toString());
                    documentationList.add(doc);
                });
                return documentationServiceImpl.InsertList(documentationList);
            }
        }
        return false;
    }

    @Override
    public Boolean DeleteNormalWarehousing(List<NormalWarehousing> normalWarehousings) {
        return this.updateBatchById(normalWarehousings);
    }

    @Override
    public List<NormalWarehousing> SelectNormalWarehousing(NormalWarehousing normalWarehousing) {
        return normalWarehousingMapper.SelectNormalWarehousing(normalWarehousing);
    }
}




